<?php
/**
 * RedXenon Framework
 * 
 * LICENSE
 * 
 * This source file is subject to the New BSD license that is bundled
 * with this package in the file LICENSE.txt
 * A copy of this license can be found in the world-wide-web at this URL:
 * http://www.redxenon.com/license/new-bsd-license.txt
 */

/* Abstract Class */
require_once("RXQuery.php");

/** 
 * RedXenon Database (RXInsert.php)
 * Class to manage Database Delete Queries
 * 
 * @category RedXenon
 * @package RedXenon_Database
 * @copyright Copyright (c) 2011 Red Xenon (http://www.redxenon.com)
 * @version $Id: RXInsert.php 1.0 2011-09-06 $ 
 */
class RXInsert extends RXQuery{

	private $fields;
	private $values;
	private $insertId;
	private $ignore;
	
	/**
	 * Constructor that calls the parent class
	 * @param RXDatabaseConnection $connection
	 * @param String $query
	 * @see RXQuery::__construct 
	 */
	public function __construct($connection = NULL, $query = NULL){
		$this->fields = array();
		$this->values = array();
		parent::__construct($connection, $query);
	}
	
	/**
	 * Destructor
	 */
	public function __destruct(){
		parent::__destruct();
	}
	
	/**
	 * Returns the set of fields
	 * @return Array.
	 */
	public function getFields(){
		return $this->fields;
	}
	
	/**
	 * Returns the set of values
	 * @return Array
	 */
	public function getValues(){
		return $this->values;
	}
	
	/**
	 * Sets the field set
	 * @param Array $fields
	 */
	public function setFields($fields){
		if(is_array($fields)){
			$this->fields = $fields;
		}else{
			$this->addField($fields);
		}
	}
	
	/**
	 * Sets the value set
	 * @param Array $values
	 */
	public function setValues($values){
		if(is_array($values)){
			$this->values = $values;
		}else{
			$this->addValue($values);
		}
	}
	
	/**
	 * Adds a field to the field set
	 * @param Mixed $field
	 */
	public function addField($field){
		array_push($this->fields, $field);
	}
	
	/**
	 * Adds a value to the value set
	 * @param Mixed $value
	 */
	public function addValue($value){
		array_push($this->values, $value);
	}
	
	/**
	 * Adds a couple field / value
	 * @param Mixed $field
	 * @param Mixed $value
	 */
	public function addFieldValue($field, $value){
		$this->addField($field);
		$this->addValue($value);
	}
	
	/**
	 * Sets the Insert condition to ignore possible 
	 * existing rows in the database and prevent the 
	 * Duplicate key error
	 */
	public function setIgnore(){
		$this->ignore = TRUE;
	}
	
	public function buildQuery(){
		/* INSERT */
		$this->query = "INSERT ";
		if($this->ignore)
			$this->query .= "IGNORE ";
		$this->query .= "INTO ";
		

		/* TABLE */
		if(is_array($this->tables)){
			$this->query .=  $this->tables[0]["tableName"];
		}else{
			$this->query .= $this->tables;
		}
		
		/* FIELDS */
		if(count($this->fields) > 0){
			$this->query .= " (";
			foreach($this->fields as $field){
				$this->query .= $field.", ";
			}
			$this->query = substr($this->query, 0, strlen($this->query) - 2);
			$this->query .= ")";
		}
		
		/* VALUES */
		$this->query .= " VALUES (";
		foreach($this->values as $value){
			$this->query .= "'".$value."', ";
		}
		$this->query = substr($this->query, 0, strlen($this->query) - 2);
		$this->query .= ")";
	}
	
	public function execute(){		
		if(!$this->query)
			$this->buildQuery();
		
		if(parent::exec()){
			$this->insertId = $this->connection->getLastInsertedId();
			return TRUE;
		}else{
			return FALSE;
		}
	}
	
	public function getLastInsertedId(){
		return $this->insertId;
	}
}

?>